home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / BORL_TIP / TI1000 / TI1779.ASC < prev   
Text File  |  1994-02-17  |  8KB  |  397 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland Pascal                        NUMBER  :  1779
  9.   VERSION  :  7.0
  10.        OS  :  DOS
  11.      DATE  :  February 17, 1994                        PAGE  :  1/6
  12.  
  13.     TITLE  :  Adding and Removing Items From A Turbo Vision Listbox
  14.  
  15.  
  16.  
  17.  
  18.  
  19.   {
  20.     Often projects will come along requiring the user to add or
  21.     remove items from a list of items.  In Turbo Vision, the
  22.     listbox is used for presenting lists.  However, there are no
  23.     built-in methods for adding or removing items from a listbox.
  24.  
  25.     It is a simple matter to manipulate collection used by the
  26.     listbox.  But in order for the listbox behave properly, the
  27.     range field of the listbox must be carefully updated.
  28.  
  29.     The unit described below details how to add and delete items
  30.     from listbox. In this example, new items are added by entering
  31.     text in an inputline and pressing the enter key.  Items are
  32.     deleted by focusing the item to be removed in the listbox and
  33.     pressing the delete key.
  34.   }
  35.   unit LBDlg;
  36.  
  37.   interface
  38.  
  39.   uses Objects, Views, Dialogs, Drivers, App;
  40.  
  41.   type
  42.     PXDlg = ^TXDlg;
  43.     TXDlg = object(TDialog)
  44.       ItemLine: PInputLine;
  45.       ItemListBox: PListBox;
  46.       ItemList: PStringCollection;
  47.       constructor Init(var Bounds: TRect; ATitle: TTitleStr);
  48.       procedure HandleEvent(var Event: TEvent); virtual;
  49.       procedure AddItem;
  50.       procedure DelItem(var Event: TEvent);
  51.     end;
  52.  
  53.   implementation
  54.  
  55.   const
  56.     cmAddItem = 100;
  57.  
  58.   constructor TXDlg.Init(var Bounds: TRect; ATitle: TTitleStr);
  59.   var
  60.     R: TRect;
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland Pascal                        NUMBER  :  1779
  75.   VERSION  :  7.0
  76.        OS  :  DOS
  77.      DATE  :  February 17, 1994                        PAGE  :  2/6
  78.  
  79.     TITLE  :  Adding and Removing Items From A Turbo Vision Listbox
  80.  
  81.  
  82.  
  83.  
  84.     AddButton: PButton;
  85.   begin
  86.     inherited Init(Bounds, ATitle);
  87.  
  88.     { Center the dialog on screen }
  89.     Options := Options or ofCentered;
  90.  
  91.     { Insert an InputLine }
  92.     R.Assign(3, 2, 28, 3);
  93.     ItemLine := New(PInputLine, Init(R, 25));
  94.     Insert(ItemLine);
  95.  
  96.     { Insert a Listbox }
  97.     R.Assign(3, 4, 28, 9);
  98.     ItemLIstBox := New(PListbox, Init(R, 1, nil));
  99.     Insert(ItemListBox);
  100.  
  101.     { Inserting a Add Button }
  102.     { The Add Button will be a default button able to respond to
  103.       the Enter key.
  104.     }
  105.     R.Assign(29, 2, 36, 4);
  106.     AddButton := New(PButton, Init(R, '~A~dd', cmAddItem,
  107.       bfDefault));
  108.     Insert(AddButton);
  109.  
  110.     { Hide the Add button }
  111.     { This step is need only if you do not wish the Add button to
  112.       appear in the dialog.
  113.     }
  114.     AddButton^.Hide;
  115.  
  116.     { Set the input focus to the Inputline }
  117.     ItemLine^.Focus;
  118.  
  119.     { Create an empty string collection for the listbox }
  120.     ItemList := New(PStringCollection, Init(5, 1));
  121.     ItemListBox^.NewList(ItemList);
  122.   end;
  123.  
  124.   procedure TXDlg.HandleEvent(var Event: TEvent);
  125.   begin
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland Pascal                        NUMBER  :  1779
  141.   VERSION  :  7.0
  142.        OS  :  DOS
  143.      DATE  :  February 17, 1994                        PAGE  :  3/6
  144.  
  145.     TITLE  :  Adding and Removing Items From A Turbo Vision Listbox
  146.  
  147.  
  148.  
  149.  
  150.     { Check for the Delete key }
  151.     { This is done before the inherited HandleEvent as TDialog's
  152.       HandleEvent will normally pass keystrokes on to the focused
  153.       control.
  154.     }
  155.     case Event.What of
  156.       evKeyDown:
  157.         case Event.KeyCode of
  158.           kbDel: DelItem(Event);
  159.         end;
  160.     end;
  161.  
  162.     { Process events normally }
  163.     inherited HandleEvent(Event);
  164.  
  165.     { Check to see if the Add button is pressed }
  166.     case Event.What of
  167.       evCommand:
  168.         case Event.Command of
  169.           cmAddItem: AddItem;
  170.         end;
  171.     end;
  172.   end;
  173.  
  174.   procedure TXDlg.AddItem;
  175.   begin
  176.     if ItemLine^.Data^ <> '' then
  177.       begin
  178.  
  179.         { Insert the InputLine's Data into the collection used by
  180.           the listbox.
  181.         }
  182.         ItemList^.Insert(NewStr(ItemLine^.Data^));
  183.  
  184.         { Adjust the range of valid values seen by the listbox }
  185.         ItemListBox^.SetRange(ItemList^.Count);
  186.  
  187.         { Clear out the Inputline }
  188.         ItemLine^.Data^ := '';  ItemLine^.CurPos := 0;
  189.  
  190.         { Force the dialog to redraw itself }
  191.         ReDraw;
  192.       end;
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland Pascal                        NUMBER  :  1779
  207.   VERSION  :  7.0
  208.        OS  :  DOS
  209.      DATE  :  February 17, 1994                        PAGE  :  4/6
  210.  
  211.     TITLE  :  Adding and Removing Items From A Turbo Vision Listbox
  212.  
  213.  
  214.  
  215.  
  216.   end;
  217.  
  218.   procedure TXDlg.DelItem(var Event: TEvent);
  219.   begin
  220.     { If the Inputline is not focused and there is something in
  221.       listbox, remove the focused item from the list used by the
  222.       listbox.
  223.     }
  224.     if (Current <> PView(ItemLine)) and (ItemList^.Count <> 0)
  225.     then
  226.       begin
  227.         { Clear the event as the DEL is being handled here }
  228.         ClearEvent(Event);
  229.  
  230.         { Release the memory used by the focused item in the
  231.           listbox
  232.         }
  233.         ItemList^.AtFree(ItemListBox^.Focused);
  234.  
  235.         { Reset the range of values seen by the listbox }
  236.         ItemListBox^.SetRange(ItemList^.Count);
  237.  
  238.         { Adjust the focused item in the listbox if necessary }
  239.         if(ItemListBox^.Range > 0) and
  240.           (ItemListBox^.Focused > ItemListBox^.Range-1)
  241.         then
  242.           ItemListbox^.Focused := ItemListBox^.Range-1;
  243.  
  244.         { if the Listbox is empty, focus the inputline }
  245.         { This step is entirely optional }
  246.         if ItemListbox^.Range = 0 then SelectNext(True);
  247.  
  248.         { For the dialog to redraw itself }
  249.         ReDraw;
  250.       end;
  251.   end;
  252.  
  253.   end.
  254.  
  255.   { Here is a small program to that uses the dialog described
  256.     above.
  257.   }
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland Pascal                        NUMBER  :  1779
  273.   VERSION  :  7.0
  274.        OS  :  DOS
  275.      DATE  :  February 17, 1994                        PAGE  :  5/6
  276.  
  277.     TITLE  :  Adding and Removing Items From A Turbo Vision Listbox
  278.  
  279.  
  280.  
  281.  
  282.   program LBAdd;
  283.  
  284.   uses
  285.     LBDlg,
  286.     Objects, Views, Dialogs, Memory, Drivers, Menus, StdDlg,
  287.     ColorSel, App;
  288.  
  289.   const
  290.     cmAction = 103;
  291.  
  292.   type
  293.     PMainApp = ^TMainApp;
  294.     TMainApp = object(TApplication)
  295.       procedure InitMenuBar; virtual;
  296.       procedure HandleEvent(var Event: TEvent); virtual;
  297.       procedure Action;
  298.     end;
  299.  
  300.   procedure TMainApp.InitMenuBar;
  301.   var
  302.     R: TRect;
  303.   begin
  304.     GetExtent(R);
  305.     R.B.Y := R.A.Y + 1;
  306.     MenuBar := New(PMenuBar, Init(R, NewMenu(
  307.       NewItem('~A~ction', '', kbNoKey, cmAction, hcNoContext,
  308.       nil)
  309.     )));
  310.   end;
  311.  
  312.   procedure TMainApp.HandleEvent(var Event: TEvent);
  313.   begin
  314.     inherited HandleEvent(Event);
  315.     case Event.What of
  316.       evCommand:
  317.         case Event.Command of
  318.           cmAction: Action;
  319.         end;
  320.     end;
  321.   end;
  322.  
  323.   procedure TMainApp.Action;
  324.   var
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.   PRODUCT  :  Borland Pascal                        NUMBER  :  1779
  339.   VERSION  :  7.0
  340.        OS  :  DOS
  341.      DATE  :  February 17, 1994                        PAGE  :  6/6
  342.  
  343.     TITLE  :  Adding and Removing Items From A Turbo Vision Listbox
  344.  
  345.  
  346.  
  347.  
  348.     R: TRect;
  349.     Dlg: PXDlg;
  350.   begin
  351.     R.Assign(0, 0, 40, 12);
  352.     New(Dlg, Init(R, 'Action Dialog'));
  353.     Application^.ExecuteDialog(Dlg, nil);
  354.   end;
  355.  
  356.   var
  357.     MainApp: TMainApp;
  358.  
  359.   begin
  360.     MainApp.Init;
  361.     MainApp.Run;
  362.     MainApp.Done
  363.   end.
  364.  
  365.  
  366.  
  367.   DISCLAIMER: You have the right to use this technical information
  368.   subject to the terms of the No-Nonsense License Statement that
  369.   you received with the Borland product to which this information
  370.   pertains.
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.